using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

using GameEngine.Content;
using GameEngine.Scene;
using GameEngine.Shaders;
using GameEngine.Input;
using GameEngine.Cameras;

namespace GameEngine
{
    public class GEBaseGame : Game
    {
        private GraphicsDeviceManager graphics;
        private ContentManager content;

        private GEPostProcessor postProcessor;
        private GEInput input;

        public GEPostProcessor PostProcessor { set { postProcessor = value; } }
        public GEInput Input { get { return input; } }

        public GEBaseGame()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        protected override void Initialize()
        {
            postProcessor = new GEPostProcessor();
            input = new GEInput(this);
            input.Initialize();

            //Make a default camera

            GECamera camera = new GECamera(graphics.GraphicsDevice.Viewport);
            GECameraManager.AddCamera("default", camera);
            GECameraManager.SetActiveCamera("default");
            GECameraManager.ActiveCamera.Zoom(5);
            GECameraManager.ActiveCamera.MoveInOrbit(0, 0);

            LoadContent();

            base.Initialize();
        }

        protected override void Draw(GameTime gameTime)
        {
            if (postProcessor.Effect != null) postProcessor.PreRender(graphics.GraphicsDevice);
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            GEObjectManager.Root.Draw(graphics.GraphicsDevice);

            base.Draw(gameTime);

            if (postProcessor.Effect != null) postProcessor.PostRender(graphics.GraphicsDevice);
        }

        protected override void LoadContent()
        {
            GEObjectManager.Root.LoadContent(graphics.GraphicsDevice, content);
            GEShaderManager.LoadContent(content);
            postProcessor.LoadContent(graphics.GraphicsDevice, content);

            GEShaderManager.GetShader("basic").Effect.Parameters["xLightDirection"].SetValue(new Vector3(10f, 10f, 10f));
            GEShaderManager.GetShader("basic").Effect.Parameters["xAmbient"].SetValue(1f);

            base.LoadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            GEObjectManager.Root.Update(gameTime);
            input.Update(gameTime);
            GECameraManager.ActiveCamera.Update();

            if(input.PressedKeys.Contains(Keys.Down))
                GECameraManager.ActiveCamera.MoveInOrbit(0,-0.1f);
            if(input.PressedKeys.Contains(Keys.Up))
                GECameraManager.ActiveCamera.MoveInOrbit(0,0.1f);
            if (input.PressedKeys.Contains(Keys.Left))
                GECameraManager.ActiveCamera.MoveInOrbit(0.1f, 0);
            if (input.PressedKeys.Contains(Keys.Right))
                GECameraManager.ActiveCamera.MoveInOrbit(-0.1f, 0);

            if(input.PressedKeys.Contains(Keys.PageUp))
                GECameraManager.ActiveCamera.Zoom(5f);
            if (input.PressedKeys.Contains(Keys.PageDown))
                GECameraManager.ActiveCamera.Zoom(-5f);

            base.Update(gameTime);
        }
    }
}
